package com.stylefeng.guns.modular.owBackstageMgr.jobMgr.controller;

import com.baomidou.mybatisplus.plugins.Page;
import com.stylefeng.guns.core.base.controller.BaseController;
import com.stylefeng.guns.core.base.tips.ErrorTip;
import com.stylefeng.guns.core.common.constant.factory.PageFactory;
import com.stylefeng.guns.core.common.exception.BizExceptionEnum;
import com.stylefeng.guns.core.exception.GunsException;
import com.stylefeng.guns.core.util.ToolUtil;
import com.stylefeng.guns.modular.system.warpper.JobWarpper;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.beans.factory.annotation.Autowired;
import com.stylefeng.guns.core.log.LogObjectHolder;
import org.springframework.web.bind.annotation.RequestParam;
import com.stylefeng.guns.modular.system.model.Job;
import com.stylefeng.guns.modular.owBackstageMgr.jobMgr.service.IJobService;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 招聘管理控制器
 *
 * @author fengshuonan
 * @Date 2018-07-01 14:55:34
 */
@Controller
@RequestMapping("/jobMgr")
public class JobMgrController extends BaseController {

    private String PREFIX = "/jobMgr/job/";

    @Autowired
    private IJobService jobService;

    /**
     * 跳转到招聘管理首页
     */
    @RequestMapping("")
    public String index() {
        return PREFIX + "job.html";
    }

    /**
     * 跳转到添加招聘管理
     */
    @RequestMapping("/job_add")
    public String jobAdd() {
        return PREFIX + "job_add.html";
    }

    /**
     * 跳转到修改招聘管理
     */
    @RequestMapping("/job_update/{jobId}")
    public String jobUpdate(@PathVariable Integer jobId, Model model) {
        Job job = jobService.selectById(jobId);
        model.addAttribute("job",job);
        LogObjectHolder.me().set(job);
        return PREFIX + "job_edit.html";
    }

    /**
     * 获取招聘管理列表
     */
    @RequestMapping(value = "/list")
    @ResponseBody
    public Object list(@RequestParam(required = false) String condition) {
        Page<Job> page = new PageFactory<Job>().defaultPage();
        List<Map<String, Object>> result = jobService.getJobList(page, condition,  page.getOrderByField(), page.isAsc());
        page.setRecords((List<Job>) new JobWarpper(result).warp());
        return super.packForBT(page);
    }

    /**
     * 新增招聘管理
     */
    @RequestMapping(value = "/add")
    @ResponseBody
    public Object add(Job job) {
        if (ToolUtil.isOneEmpty(job)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }
        job.setCreateTime(new Date());
        job.setUpdateTime(new Date());
        job.setDuty(job.getDuty().replace("& lt;","<").replace("& gt;",">"));
        job.setRequirement(job.getRequirement().replace("& lt;","<").replace("& gt;",">"));
        job.setWelfare(job.getWelfare().replace("& lt;","<").replace("& gt;",">"));
        job.insert();
        return SUCCESS_TIP;
    }

    /**
     * 删除招聘管理
     */
    @RequestMapping(value = "/delete")
    @ResponseBody
    public Object delete(@RequestParam Integer jobId) {
        if (jobId != null){
            Job job = jobService.selectById(jobId);
            if (job != null){
                job.setUpdateTime(new Date());
                job.setIsDel(1);// 表示删除状态
                job.updateById();
                return SUCCESS_TIP;
            }else {
                return new ErrorTip(-1,"找不到需要删除的记录");
            }
        }
        return new ErrorTip(-1,"参数错误");
    }

    /**
     * 修改招聘管理
     */
    @RequestMapping(value = "/update")
    @ResponseBody
    public Object update(Job job) {
        if (ToolUtil.isOneEmpty(job)) {
            throw new GunsException(BizExceptionEnum.REQUEST_NULL);
        }

        Job old = this.jobService.selectById(job.getId());
        old.setName(job.getName());
        old.setCount(job.getCount());
        old.setWorkYear(job.getWorkYear());
        old.setEducation(job.getEducation());
        old.setWorkplace(job.getWorkplace());
        old.setSalary(job.getSalary());
        old.setDuty(job.getDuty().replace("& lt;","<").replace("& gt;",">"));
        old.setRequirement(job.getRequirement().replace("& lt;","<").replace("& gt;",">"));
        old.setWelfare(job.getWelfare().replace("& lt;","<").replace("& gt;",">"));
        old.setUpdateTime(new Date());

        old.updateById();
        return SUCCESS_TIP;
    }

    /**
     * 招聘管理详情
     */
    @RequestMapping(value = "/detail/{jobId}")
    @ResponseBody
    public Object detail(@PathVariable("jobId") Integer jobId) {
        return jobService.selectById(jobId);
    }
}
